﻿using KamRemakeRemake.GameObjects;
using KamRemakeRemake.GameObjects.MapObjects;
using KamRemakeRemake.GameObjects.UnitObjects.ActionObjects.Actions;
using KamRemakeRemake.GameObjects.UnitObjects.Units;
using KamRemakeRemake.GameObjects.UnitObjects.Units.BattleUnits;
using KamRemakeRemake.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace KamRemakeNET.GameObjects.Players {
    public class HumanPlayer : Player {
        private MouseState oldMouseState;

        public Unit SelectedUnit { get; private set; }

        public HumanPlayer(Map map) : base(map) {
            // fighter test
            Units.Add(new Fighter(100, 100));
            Units.Add(new Fighter(120, 100));
            Units.Add(new Fighter(200, 100));

            //Units[0].DoAction(new Charge(Units[0], new Vector2(100, 400)));
            //Units[1].DoAction(new Walk(Units[1], new Vector2(120, 420)));
            Units[2].DoAction(new Face(Units[2], Direction.East));
            
            // bowman test
            Units.Add(new Bowman(200, 200));
            Units.Add(new Bowman(175, 220));
            Units.Add(new Bowman(150, 240));
            Units.Add(new Bowman(240, 205));
            Units.Add(new Bowman(220, 230));
            Units.Add(new Bowman(200, 250));

            Units[3].DoAction(new Attack(Units[3], null));
            Units[4].DoAction(new Attack(Units[4], null));
            Units[5].DoAction(new Attack(Units[5], null));
            Units[6].DoAction(new Attack(Units[6], null));
            Units[7].DoAction(new Attack(Units[7], null));
            Units[8].DoAction(new Attack(Units[8], null));
        }

        public override void Update(GameTime gameTime) {
            base.Update(gameTime);
            this.HandleInput();
        }

        private void HandleInput() {
            MouseState newMouseState = Mouse.GetState();
            Rectangle cursorRectangle = Cursor.Instance.ScreenRectangle;
            cursorRectangle.X += (int)Camera.Instance.Position.X;
            cursorRectangle.Y += (int)Camera.Instance.Position.Y;

            foreach (Unit unit in this.Units) {
                // Display the select icon when we hover over a unit
                if (cursorRectangle.Intersects(unit.Rectangle)) {
                    Cursor.Instance.SetCursor(Cursor.CursorType.Select);
                    // When we click on a unit, select him
                    if (newMouseState.LeftButton == ButtonState.Pressed) {
                        this.SelectedUnit = unit;
                    }
                }
            }

            // If we click the right mouse button when we have a selected unit, order him to move
            if (this.SelectedUnit != null) {
                if (newMouseState.RightButton == ButtonState.Released && oldMouseState.RightButton == ButtonState.Pressed) {
                    // Get the map coordinates that were clicked
                    Vector2 targetPosition = new Vector2(
                        newMouseState.X + (int)Camera.Instance.Position.X, 
                        newMouseState.Y + (int)Camera.Instance.Position.Y
                    );

                    // Get the tile thats at the clicked coordinates
                    Tile targetTile = this.Map.GetTileByCoordinates(targetPosition);

                    // Order the unit to move there
                    this.SelectedUnit.DoAction(new Walk(this.SelectedUnit, targetTile));
                }
            }

            oldMouseState = newMouseState;
        }
    }
}
